package cn.wonderland.controller;

import cn.wonderland.domain.User;
import cn.wonderland.service.UserService;
import cn.wonderland.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public JSONResult<User> addUser(@RequestBody User user) {
        if(user.getName() == null || user.getPhone() == null){
            return JSONResult.<User>builder()
                    .message("姓名或手机号不能为空！")
                    .code(400)
                    .success(false)
                    .data(null)
                    .build();
        }
        userService.addUser(user);
        return JSONResult.<User>builder()
                .message("请求成功")
                .code(200)
                .success(true)
                .data(null)
                .build();
    }

    @GetMapping("/getUserByUserId/{id}")
    public JSONResult<User> getUserByUserId(@PathVariable("id") Long id) {
        User user = userService.getUserByUserId(id);
        return JSONResult.<User>builder()
                .message("请求成功")
                .code(200)
                .success(true)
                .data(user)
                .build();
    }
}
